⚡️ Speed up function _equilibrium_payoffs_abreu_sannikov by 13%
#26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 13% (0.13x) speedup for
_equilibrium_payoffs_abreu_sannikovinquantecon/game_theory/repeated_game.py⏱️ Runtime :
665 milliseconds→587 milliseconds(best of7runs)📝 Explanation and details
The optimized code achieves a 13% speedup through several targeted performance improvements that reduce overhead and improve memory access patterns:
Key Optimizations:
Reduced attribute access overhead: Pre-extracted
hull.points,hull.vertices, andhull.equationsbefore passing to the njit-compiled_Rfunction, eliminating repeated attribute lookups inside the performance-critical loop.Eliminated redundant computations: Cached
np.prod(sg.nums_actions)asn_actto avoid recalculating this value multiple times throughout the function.Optimized convergence checking: Replaced expensive
np.linalg.norm()with a more efficient sum-of-squares calculation usingnp.sqrt(np.sum(arr_diff * arr_diff)), reducing computational overhead in the convergence test.Enhanced Numba compilation: Added
cache=Trueandfastmath=Trueto njit decorators, enabling better optimization and caching of compiled functions.Improved array operations in
_R:(action_profile_payoff >= IC).all()with explicit element-wise comparisons, which performs better in NumbaGenerator elimination: In
_best_dev_gains, replaced generator expression with tuple comprehension to avoid iterator overhead and ensure immediate computation.Performance Impact by Test Case:
The optimizations are particularly effective for larger action spaces where the nested loops in
_Rdominate execution time, making the memory access and compilation improvements most beneficial.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_equilibrium_payoffs_abreu_sannikov-mgh4ih42and push.